package com.eqxiu.ieditor.api.controller.order;


import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.eqxiu.ieditor.api.controller.BaseApiController;
import com.eqxiu.ieditor.api.order.IOrderServiceApi;
import com.eqxiu.ieditor.commons.web.MsgCode;
import com.eqxiu.ieditor.commons.web.ResultData;
import com.eqxiu.ieditor.dto.OrderDTO;
import com.eqxiu.ieditor.dto.OrderTypeEnum;
import com.eqxiu.ieditor.utils.WebUtil;
import com.eqxiu.ieditor.weixin.api.StylishProductServiceApi;
import com.eqxiu.ieditor.weixin.api.UnifiedOrderServiceApi;
import com.eqxiu.ieditor.weixin.dto.StylishProduct;
import com.eqxiu.ieditor.weixin.dto.UnifiedOrder;

/**
 * 
 * @description 爆款游戏的支付,直接对接微信,没有使用易企秀支付平台
 * @author ranger
 * @date Aug 1, 2017
 */
@RestController
public class OrderController implements BaseApiController {

    @Autowired
    private IOrderServiceApi orderServiceApi;


    @Autowired
    private UnifiedOrderServiceApi unifiedOrderServiceApi;

    /*@Autowired
    private StylishProductDao stylishProductDao;*/
    
    @Autowired
    private StylishProductServiceApi stylishProductServiceApi;

    @PostMapping("/api/stylish/order/create")
    public ResultData createOrder(@Valid @RequestBody UnifiedOrder order, HttpServletRequest req) throws Exception {

        OrderDTO orderDTO = new OrderDTO();
        orderDTO.setCreateUser(order.getOpenId());
        // 代表小游戏
        orderDTO.setPayType(OrderTypeEnum.STYLISH.getState());

        orderDTO.setName(order.getBody());

        /*StylishProduct product = stylishProductDao.selectByPrimaryKey(Integer.parseInt(order.getProductId()));*/
        StylishProduct product = stylishProductServiceApi.getById(Integer.parseInt(order.getProductId()));
        
        if (product == null || product.getPrice() <= 0) {
            return fail().setCode(MsgCode.DATA_NOT_EXIST).setMsg("DATA_NOT_EXIST or price <=0");
        }

        orderDTO.setProductId(product.getId());
        orderDTO.setTotalFee(product.getPrice());
        orderServiceApi.createOrder(orderDTO);
        order.setOutTradeNo("" + orderDTO.getId());
        // hard code
        order.setSpbillCreateIp(WebUtil.getIpAddress(req));
        order.setTotalFee(product.getPrice());
        Map<String, Object> result = unifiedOrderServiceApi.unifiedOrder(order);

        return success().setMap(result).setObj(orderDTO);
    }

    /**
     * @description 处理微信的回调信息
     * @author DuYoulong
     * @modified 2019-04-10 服务化重构，@Deprecated原接口，使用新的接口。将数据转换操作提取到service中。
    */
    @RequestMapping(value = "/api/stylish/notify-async", method = {RequestMethod.GET, RequestMethod.POST})
    public void handleNotify(@RequestBody String result, HttpServletResponse resp) throws Exception {
       /* Map<String, String> map = WXPayUtil.xmlToMap(result);
        log.info("receive notify {}", map);
        String state = unifiedOrderService.handleNotify(map);*/
    	
        unifiedOrderServiceApi.handleNotify(result);

       /* if (WXPayConstants.SUCCESS.equals(state)) {
            OrderDTO dto = orderServiceApi.getOrderById(Long.valueOf(map.get("out_trade_no")));
            if (null == dto) {
                log.error("never find order {}", map.get("out_trade_no"));
                resp.getWriter().write(WXPayConstants.FAIL);
                return;
            }
            dto.setStatus(OrderStatusEnum.PAID.status());
            dto.setTransactionId(map.get("transaction_id"));
            orderServiceApi.finishOrder(dto);
            log.info("finish order , return weixin {} ", WXPayConstants.SUCCESS);
            resp.getWriter().write(WXPayConstants.SUCCESS);
        }*/
    }
}
